package modelLayer;
import java.util.*;



public class PersonContainer
{
    // instance variables
	private static int index = 0;
	private static PersonContainer instance;
	private ArrayList<Person> persons;

   
    private PersonContainer()
    {
        persons=new ArrayList();
    }
    
    public static PersonContainer getInstance()
    {
        if(instance==null)
        {
            instance = new PersonContainer();
        }
        return instance;
    }
    /**
     * The method cheks if there are any persons.
     * @return boolean
     */
    public boolean isEmpty()
    {
        return persons==null;
    }
    public void addPerson(Person person)
    {
    	person.setId(++index);
        persons.add(person);
    }
    public void deletePerson(Person person)
    {
        persons.remove(person);
    }
    /**
     * The method searches for person by its Id
     * @return boolean
     */
    public Person getPerson(int id)
    {
        
        
        if(!isEmpty())
        { boolean found=false;
            int index=0;
        while(!found&&index<persons.size())
        {
            if(persons.get(index).getId()==id)
            {
                found=true;
                return persons.get(index);
            }
            index++;
        }
        }
        return null;
    }
    public ArrayList<Person> getAll(){
    	return persons;
    }
}
